package service

func AddUser(uid int, name string, dept int) error {
	ok := ""
	return ReqSvc("user", "AddUser", map[string]interface{}{
		"Uid":  uid,
		"Name": name,
		"Dept": dept,
	}, &ok)
}

func UpdateUser(uid int, name string, dept int) error {
	ok := ""
	return ReqSvc("user", "UpdateUser", map[string]interface{}{
		"Uid":  uid,
		"Name": name,
		"Dept": dept,
	}, &ok)
}

func RemoveUser(uid int) error {
	ok := ""
	return ReqSvc("user", "RemoveUser", map[string]interface{}{
		"Uid": uid,
	}, &ok)
}

func AddDept(name string, leader, order int) (int, error) {
	id := 0
	err := ReqSvc("user", "AddDept", map[string]interface{}{
		"Name":   name,
		"Leader": leader,
		"Order":  order,
	}, &id)
	return id, err
}

func UpdateDept(did int, name string, leader, order int) error {
	ok := ""
	return ReqSvc("user", "UpdateDept", map[string]interface{}{
		"Did":    did,
		"Name":   name,
		"Leader": leader,
		"Order":  order,
	}, &ok)
}

func RemoveDept(did int) error {
	ok := ""
	return ReqSvc("user", "RemoveDept", map[string]interface{}{
		"Did": did,
	}, &ok)
}

func AddDeptUser(did, uid, leader int, title string) (int, error) {
	id := 0
	err := ReqSvc("user", "AddDeptUser", map[string]interface{}{
		"Did":    did,
		"Uid":    uid,
		"Leader": leader,
		"Title":  title,
	}, &id)
	return id, err
}

func RemoveDeptUser(did, uid int) error {
	ok := ""
	return ReqSvc("user", "RemoveDeptUser", map[string]interface{}{
		"Did": did,
		"Uid": uid,
	}, &ok)
}

func UpdateDeptUser(did, uid, leader int, title string) error {
	ok := ""
	return ReqSvc("user", "UpdateDeptUser", map[string]interface{}{
		"Did":    did,
		"Uid":    uid,
		"Leader": leader,
		"Title":  title,
	}, &ok)
}

func SwapDeptUser(did, uid1, uid2 int) error {
	ok := ""
	return ReqSvc("user", "SwapDeptUser", map[string]int{
		"Did":  did,
		"Uid1": uid1,
		"Uid2": uid2,
	}, &ok)
}

func GetDeptUserUids(did int) ([]int, error) {
	uids := []int{}
	err := ReqSvc("user", "GetDeptUserUids", map[string]interface{}{
		"Did": did,
	}, &uids)
	return uids, err
}

func GetUserDeptDids(uid int) ([]int, error) {
	dids := []int{}
	err := ReqSvc("user", "GetUserDeptDids", map[string]interface{}{
		"Uid": uid,
	}, &dids)
	return dids, err
}

type DeptUser struct {
	Id        int
	Did       int
	Uid       int
	Leader    int
	Title     string
	Followers []*DeptUser `json:",omitempty"`
}

func GetDeptUserTree(did int) (*DeptUser, error) {
	tree := &DeptUser{}
	err := ReqSvc("user", "GetDeptUserTree", map[string]interface{}{
		"Did": did,
	}, &tree)
	return tree, err
}

func GetDeptUserList(did int) ([]*DeptUser, error) {
	list := []*DeptUser{}
	err := ReqSvc("user", "GetDeptUserList", map[string]interface{}{
		"Did": did,
	}, &list)
	return list, err
}
